﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    //Controls enum holding names of all our user controls
    public enum WarehouseControlsEnum
    {
        DISPATCH_NOTE_CONTROL,
        RECEIVE_NOTE_CONTROL
    }

    public partial class WarehouseMainForm : Form
    {
        private bool warehouseListOfProdutsLoaded = false;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();

        //Dictionary that holds all our instantiated user controls.
        private IDictionary<WarehouseControlsEnum, Control> warehouseControls = new Dictionary<WarehouseControlsEnum, Control>();

        public WarehouseMainForm()
        {
            InitializeComponent();
            //lblActiveUser.Text = Program.GetActiveUser;
            dgvWarehouseProductsList.AutoGenerateColumns = false;

            if (!warehouseListOfProdutsLoaded)
            {
                loadWarehouseListOfProduts();
                warehouseListOfProdutsLoaded = true;
            }
        }

        private void loadWarehouseListOfProduts()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            string[] row;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();


            string sqlSelectAll = "SELECT * from Products";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            //nov kod
            DataSet table = new DataSet();
            MyDA.Fill(table);

            dgvWarehouseProductsList.ColumnCount = 6;
            dgvWarehouseProductsList.Columns[0].Name = "Категорија 1";
            dgvWarehouseProductsList.Columns[1].Name = "Категорија 2";
            dgvWarehouseProductsList.Columns[2].Name = "Категорија 3";
            dgvWarehouseProductsList.Columns[3].Name = "Име";
            dgvWarehouseProductsList.Columns[4].Name = "Part Num.";
            dgvWarehouseProductsList.Columns[5].Name = "Цена";

            foreach (DataRow Product in table.Tables[0].Rows)
            {
                row = new string[] { Product[1].ToString(), Product[2].ToString(), Product[3].ToString(), Product[4].ToString(), Product[5].ToString(), Product[6].ToString() };
                dgvWarehouseProductsList.Rows.Add(row);
            }

            mysqlCon.Close();

        }

        public void ShowControl(WarehouseControlsEnum ctrl)
        {
            Control new_ctrl = null;

            //If our dictionary already contains instance of
            if (warehouseControls.ContainsKey(ctrl))
            {
                new_ctrl = warehouseControls[ctrl];
            }
            else
            {
                new_ctrl = CreateControl(ctrl);
                warehouseControls[ctrl] = new_ctrl;
            }

            new_ctrl.Parent = this;
            new_ctrl.Dock = DockStyle.Fill;
            new_ctrl.BringToFront();
            new_ctrl.Show();
        }

        private Control CreateControl(WarehouseControlsEnum ctrl)
        {
            switch (ctrl)
            {
                case WarehouseControlsEnum.DISPATCH_NOTE_CONTROL:
                    return new NewDispatchNoteControl();
                case WarehouseControlsEnum.RECEIVE_NOTE_CONTROL:
                    return new NewReceiveNoteControl();
                default:
                    return null;
            }
        }

        private void btnNewReceiveNote_Click(object sender, EventArgs e)
        {
            ShowControl(WarehouseControlsEnum.RECEIVE_NOTE_CONTROL);
        }

        private void btnNewDispatchNote_Click(object sender, EventArgs e)
        {
            ShowControl(WarehouseControlsEnum.DISPATCH_NOTE_CONTROL);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
